package com.admin.scroller.photoorvideo.fragment;

import android.annotation.SuppressLint;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.admin.scroller.photoorvideo.R;
import com.admin.scroller.photoorvideo.viewModel.PhotoViewModel;
import com.bumptech.glide.Glide;

import java.util.Objects;

import static android.widget.AbsListView.OnScrollListener.SCROLL_STATE_IDLE;

/**
 * 图片选择的基类fragment
 */
public abstract class BasePhotoFragment extends Fragment {

    protected View fview;
    protected PhotoViewModel viewModel;
    protected RecyclerView recyclerView;
    protected LinearLayout linearBody;
    protected LinearLayout linearBar;
    protected TextView textDate;

    protected boolean onTouchBar=false;//是否为滑块滑动
    protected Handler handler=new Handler();
    protected int scrollerState;//滑动的状态
    protected int progress;//滑动进度
    protected float berY;//其实bar滑动的按下位置

    protected int dataSize;//列表的总数

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fview=inflater.inflate(R.layout.fragment_body,container,false);
        initView();
        initData();
        initEvent();
        return fview;
    }

    private void initView(){
        recyclerView =fview.findViewById(R.id.rlv_photos);
        linearBody=fview.findViewById(R.id.linear_body);
        linearBar=fview.findViewById(R.id.linear_bar);
        textDate=fview.findViewById(R.id.tv_time_date);
        recyclerView.getItemAnimator().setChangeDuration(0);
        viewModel = ViewModelProviders.of(getActivity()).get(PhotoViewModel.class);
    }

    abstract void initData();

    abstract void getAllHeight();

    @SuppressLint("ClickableViewAccessibility")
    private void initEvent(){

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState==RecyclerView.SCROLL_STATE_IDLE){
                    Glide.with(Objects.requireNonNull(getContext())).resumeRequests();
                }else {
                    Glide.with(Objects.requireNonNull(getContext())).pauseRequestsRecursive();
                }
                scrollerState=newState;
                changeBar();
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                getProgress();
            }
        });

        linearBar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        onTouchBar=true;
                        textDate.setVisibility(View.VISIBLE);
                        berY=event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        onTouchBar=false;
                        break;
                }
                return false;
            }
        });

        linearBody.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if(onTouchBar) {
                            changeBarLocation(event.getY());
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if(onTouchBar) {
                            onTouchBar=false;
                            textDate.setVisibility(View.GONE);
                            setLinearBarLocation((int) (event.getY() - linearBar.getHeight() / 2));
                            changeBar();
                        }
                        break;
                }
                return true;
            }
        });
    }

    //设置bar的显示与隐藏
    private void changeBar(){
        if(scrollerState==SCROLL_STATE_IDLE){
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(!onTouchBar && scrollerState==SCROLL_STATE_IDLE) {
                        linearBar.setVisibility(View.GONE);
                    }
                }
            },1000);
        }else {
            linearBar.setVisibility(View.VISIBLE);
        }
    }

    abstract void getProgress();

    //设置bar的滑动位置
    private void changeBarLocation(final float y){
        setLinearBarLocation((int)(y-berY));
        //设置view
        if(onTouchBar) {
            int p = (int) (dataSize * (y / linearBody.getHeight()));
            listScrollToPosition(p);
        }
    }

    //设置bar的位置
    protected void setLinearBarLocation(int top){
        if(top<0){ top=0;}else if(top+linearBar.getHeight()>linearBody.getHeight()) top=linearBody.getHeight()-linearBar.getHeight();
        LinearLayout.LayoutParams params=(LinearLayout.LayoutParams) linearBar.getLayoutParams();
        params.setMargins(params.leftMargin,top,params.rightMargin,0);
        linearBar.setLayoutParams(params);
    }

    //滑动到指定的item
    private void listScrollToPosition(int index){
        if(index>=dataSize){
            index=dataSize-1;
        }else if(index<0) {
            index=0;
        }
        recyclerView.smoothScrollToPosition(index);
        changeTextDate(index);
    }

    abstract void changeTextDate(int index);

}
